package com.luxury.fragment.party_fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luxury.HomeWebActivity;
import com.luxury.MainActivity;
import com.luxury.R;
import com.luxury.adpter.Tablayou_ViewPagerAdapter;
import com.luxury.bean.ArrglobaBean;
import com.luxury.r_innerface.InterfaceLuxury;
import com.luxury.utils.URLContants;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import static com.luxury.adpter.HomeRecyclerAdapter1.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class GlobalFragment extends Fragment {
    /*    private ViewPager viewPager;
        private TabLayout tabLayout;
        private String[]   mArrTabglobalID=null;
        private String[] mArrTabglobal = null;
        private List<Fragment> mListFragments = new ArrayList<>();
        private List<ArrglobaBean.RpResultBean.CategoryInfoBean> mlist = new ArrayList<>();*/
    private String golbalUrl = "http://android.secoo.com/appActivity/global.shtml?pageid=1088";
    private WebView Web_golbal;


    public GlobalFragment() {
        // Required empty public constructor
    }

    public static GlobalFragment newInstance() {
        Bundle args = new Bundle();
        GlobalFragment fragment = new GlobalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_global_h5, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();

    }

    private void initData() {
        Web_golbal.loadUrl(golbalUrl);
        Web_golbal.getSettings().setJavaScriptEnabled(true);
        Web_golbal.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent=new Intent(getActivity(), HomeWebActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });

    }

    private void initView(View view) {
        Web_golbal = (WebView) view.findViewById(R.id.webView_globalFragment_h5);
    /*      GlobalFragment globalFragment=new GlobalFragment();

    Bundle bundle = globalFragment.getArguments();
        KeyEvent keyevent = bundle.getParcelable("keyevent");
        int keyCode = bundle.getInt("keyCode");*/

    }

 /*   *//**
     * 改写返回键的返回逻辑
     *
     * @param keyCode
     * @param event
     * @return
     *//*

   public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (Web_golbal.canGoBack()) {
                Web_golbal.goBack();//返回网页的上一页
                return true;
            }
        }
        return getActivity().onKeyDown(keyCode, event);
}*/
    /*
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_global, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    *//*
    * 加载网络数据
    * *//*
    private void initData() {
        //利用retrofit请求网络数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URLContants.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create()).build();
        InterfaceLuxury interfaceLuxury=retrofit.create(InterfaceLuxury.class);
        //获取全球数组json字符串数据
        Call<String> ArrglobalString= interfaceLuxury.getArrglobalString();
        ArrglobalString.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String str = response.body();
                int begin = str.indexOf("{");
                int end = str.lastIndexOf(")");
                String jsonString = str.substring(begin,end);
                String all="{\"name\":\"全部\",\"id\":\"\"},";
                int beg =jsonString.indexOf("[")+1;
                StringBuffer stringBuffer = new StringBuffer(jsonString);
                String string = stringBuffer.insert(beg, all).toString();
                Log.i(TAG, "onResponse:---------- "+string);
                ArrglobaBean arrgloba = parseJsonToBean(string);
                mlist = arrgloba.getRp_result().getCategoryInfo();

                mArrTabglobal=new String[mlist.size()];

                for (int i = 0; i < mlist.size(); i++) {

                    mArrTabglobal[i]=mlist.get(i).getName();

                    Log.d("fdhg", "onResponse:+++++++++++ "+mArrTabglobal);

                    GlobalFragment_Child fragment =  GlobalFragment_Child.newInstance();
                    Bundle bundle = new Bundle();
                    bundle.putInt("index", i);
                    bundle.putString("ID",mlist.get(i).getId());
                    fragment.setArguments(bundle);
//            FragmentDemo fragmentDemo = new FragmentDemo();
                    mListFragments.add(fragment);
                }
                Tablayou_ViewPagerAdapter adapter = new Tablayou_ViewPagerAdapter(getActivity().getSupportFragmentManager(), mListFragments, mArrTabglobal);
                viewPager.setAdapter(adapter);
                viewPager.setOffscreenPageLimit(mlist.size());
                tabLayout.setupWithViewPager(viewPager);
            }


            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }



    private void initView(View view) {
        tabLayout = (TabLayout) view.findViewById(R.id.tablaout_fragment_global);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_global);
    }

    //Gson解析
    public ArrglobaBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<ArrglobaBean>() {
        }.getType());
    }*/
}
